<?php

namespace Api\Controller;
use Think\Controller;

class UserController extends SuperController
{

    //不允许修改字段
    protected $no_allow_update_field = array(
        'amount',
    );
  
    public function detailcc(){
        $u_model = M('User');
        $u_wdata['uid'] = I('uid');
        $info = $u_model->where($u_wdata)->field('uid as aId,username as aUser,names as aName,mobile as aTel,sex as aSex')->find();
        if($info){
            if(empty($info['aSex'])){
                $info['aSex']=0;
            }
            $data_cc['code'] = 200;
            $data_cc['msg'] = "查询成功";
            $data_cc['data'] = $info;
            exit(json_encode($data_cc));
        }else{
            $data_cc['code'] = 0;
            $data_cc['msg'] = "信息查询失败";
            exit(json_encode($data_cc));
        }
    }
    //更新信息
    public function updateAction(){
        $detail= I('post.');
        $aId=$detail['aId'];
        $aName=$detail['aName'];
        $nPwd=$detail['aNewPwd'];
        $aTel=$detail['aTel'];
        $aSex=$detail['aSex'];
        $token=$detail['token'];
        $token_model= M("token");
        $user_model= M("user");
        if(empty($aName)){
            $data_cc['code'] = 0;
            $data_cc['msg'] = "请输入姓名";
            exit(json_encode($data_cc));
        }
        if($nPwd && strlen($nPwd)<6){
            $data_cc['code'] = 0;
            $data_cc['msg'] = "请输入6位数以上密码";
            exit(json_encode($data_cc));
        }
        $data['names']=$aName;
        $data['sex']=$aSex;
        $data['mobile']=$aTel;
        $data['sex']=$aSex;
        if($aTel){
            $wherefc['mobile']=$aTel;
            $wherefc['uid']=['neq',$aId];
            $ct=$user_model->where($wherefc)->count();
            if($ct){
                $data_cc['code'] = 0;
                $data_cc['msg'] = "该手机号已存在了,请重新填写手机号码。";
                exit(json_encode($data_cc));
            }
        }
        $u_info=$user_model->where("uid=$aId")->find();
        if($nPwd){
            $salt= $u_info['salt']; 
            $data['salt']=$salt;
            $pass=$this->getEfassword($nPwd ,$salt);
            if($u_info['password']==$pass){
                $data_cc['code'] = 0;
                $data_cc['msg'] = "该密码和原密码一致,请更换密码。";
                exit(json_encode($data_cc));
            };
            $data['password']=$pass;
        }
       
        $info=$token_model->where(['token'=>$token])->find();
        if($info){
            $result = $user_model->where("uid=$aId")->save($data);
            if($result) {
                $data_cc['code'] = 200;
                $data_cc['msg'] = "修改成功";
                exit(json_encode($data_cc));
            } else {
                $data_cc['code'] = 0;
                $data_cc['msg'] = "修改失败";
                exit(json_encode($data_cc));
            }
        }else{
            $data_cc['code'] = 0;
            $data_cc['msg'] = "token不存在";
            exit(json_encode($data_cc));
        }
    }
    function GetddStr($length){
        $str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890';
        $randStr = str_shuffle($str);//打乱字符串
        $rands= substr($randStr,0,$length);//substr(string,start,length);返回字符串的一部分
        return $rands;
    }
    function getEfassword($password, $salt = ''){
        return md5(md5($password) . $salt);
    }
    function changeAmount(){
        $model = D('User');
        $model->startTrans();
        $res = $model->changeAmount(I('post.'));
        if(!$res['status']){
            $this->ajaxErrReturn($res['msg']);
        }
        $model->commit();
        $this->ajaxSucReturn($res);
    }

    //资金类型
    function getUtype(){
        $this->ajaxSucReturn(u_type());
    }

    //更新透支额度
    function credit(){
        $u_model = M('User');
        $u_wdata['uid'] = I('uid');
        $u_sdata['credit'] = I('credit');
        $res = $u_model->where($u_wdata)->save($u_sdata);
        if($res===false){
            $this->ajaxErrReturn('修改失败');
        }
        $u_vo = $u_model->find(I('uid'));
        $this->ajaxSucReturn($u_vo);
    
    }

}
